package com.yupi.yupicturebackend.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.yupi.yupicturebackend.model.entity.AIGenerationTask;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * AI图片生成任务Mapper
 *
 * @author yupi
 */
@Mapper
public interface AIGenerationTaskMapper extends BaseMapper<AIGenerationTask> {

    /**
     * 根据用户ID查询生成任务列表
     *
     * @param userId 用户ID
     * @param limit 限制数量
     * @return 任务列表
     */
    @Select("SELECT * FROM ai_generation_task WHERE userId = #{userId} AND isDelete = 0 ORDER BY createTime DESC LIMIT #{limit}")
    List<AIGenerationTask> selectByUserId(@Param("userId") Long userId, @Param("limit") Integer limit);

    /**
     * 根据状态查询任务列表
     *
     * @param status 任务状态
     * @param limit 限制数量
     * @return 任务列表
     */
    @Select("SELECT * FROM ai_generation_task WHERE status = #{status} AND isDelete = 0 ORDER BY createTime ASC LIMIT #{limit}")
    List<AIGenerationTask> selectByStatus(@Param("status") String status, @Param("limit") Integer limit);

    /**
     * 统计用户生成任务数量
     *
     * @param userId 用户ID
     * @return 任务数量
     */
    @Select("SELECT COUNT(*) FROM ai_generation_task WHERE userId = #{userId} AND isDelete = 0")
    Long countByUserId(@Param("userId") Long userId);

    /**
     * 统计各状态任务数量
     *
     * @return 状态统计
     */
    @Select("SELECT status, COUNT(*) as count FROM ai_generation_task WHERE isDelete = 0 GROUP BY status")
    List<java.util.Map<String, Object>> countByStatus();

    /**
     * 查询热门提示词
     *
     * @param limit 限制数量
     * @return 热门提示词列表
     */
    @Select("SELECT prompt, COUNT(*) as count FROM ai_generation_task WHERE isDelete = 0 AND status = 'completed' GROUP BY prompt ORDER BY count DESC LIMIT #{limit}")
    List<java.util.Map<String, Object>> selectPopularPrompts(@Param("limit") Integer limit);

    /**
     * 查询用户最近使用的模型
     *
     * @param userId 用户ID
     * @param limit 限制数量
     * @return 模型列表
     */
    @Select("SELECT DISTINCT model FROM ai_generation_task WHERE userId = #{userId} AND isDelete = 0 ORDER BY createTime DESC LIMIT #{limit}")
    List<String> selectRecentModelsByUserId(@Param("userId") Long userId, @Param("limit") Integer limit);

    /**
     * 查询高质量生成结果
     *
     * @param minQualityScore 最低质量分数
     * @param limit 限制数量
     * @return 高质量任务列表
     */
    @Select("SELECT * FROM ai_generation_task WHERE qualityScore >= #{minQualityScore} AND status = 'completed' AND isDelete = 0 ORDER BY qualityScore DESC LIMIT #{limit}")
    List<AIGenerationTask> selectHighQualityTasks(@Param("minQualityScore") Double minQualityScore, @Param("limit") Integer limit);
}
